﻿using OpenTK.Graphics.OpenGL4;

namespace TrinityEngine
{
    public class StaticIndexBuffer : IDisposable
    {
        protected bool _disposed = false;

        private int _rendererID;

        private int _count;

        public StaticIndexBuffer(uint[] data)
        {
            _rendererID = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, _rendererID);
            GL.BufferData(BufferTarget.ElementArrayBuffer, data.Length * sizeof(uint), data, BufferUsageHint.StaticDraw);
            _count = data.Length;
        }

        ~StaticIndexBuffer()
        {
            Dispose(false);
        }

        public int GetCount()
        {
            return _count;
        }

        public void Bind()
        {
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, _rendererID);
        }

        public void Unbind()
        {
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                
            }

            GL.DeleteBuffer(_rendererID);

            _disposed = true;
        }
    }
}
